package com.da.javatest.leetCode2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Code23 {
    private List<Integer> list = new ArrayList<>();

    public ListNode mergeKLists(ListNode[] lists) {
        if (lists.length < 1) {
            return null;
        }
        for (ListNode listNode : lists) {
            setList(listNode);
        }
        if (list.size() == 0) {
            return null;
        }
        Collections.sort(list);
        ListNode root = new ListNode(list.get(0));
        setNode(root, 1);
        return root;
    }

    private void setNode(ListNode root, int index) {
        if (index < list.size()) {
            root.next = new ListNode(list.get(index));
            setNode(root.next, index += 1);
        }
    }

    private void setList(ListNode node) {
        if (node != null) {
            list.add(node.val);
            setList(node.next);
        }
    }
}
